/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mapbox.ohos.gestures.ohan;

import ohos.agp.utils.Point;

import java.io.Serializable;

/**
 * @since 2021-07-02
 */
public class PointF implements Serializable {
    /**
     * pointX
     */
    public float pointX;
    /**
     * pointY
     */
    public float pointY;

    /**
     * PointF
     */
    public PointF() {
    }

    /**
     * PointF
     *
     * @param pointX
     * @param pointY
     */
    public PointF(float pointX, float pointY) {
        this.pointX = pointX;
        this.pointY = pointY;
    }

    /**
     * PointF
     *
     * @param point
     */
    public PointF(Point point) {
        this.pointX = point.getPointX();
        this.pointY = point.getPointY();
    }

    /**
     * Set the point's x and y coordinates
     *
     * @param pointX
     * @param pointY
     */
    public final void set(float pointX, float pointY) {
        this.pointX = pointX;
        this.pointY = pointY;
    }

    /**
     * Set the point's x and y coordinates to the coordinates of p
     *
     * @param pointF
     */
    public final void set(PointF pointF) {
        this.pointX = pointF.pointX;
        this.pointY = pointF.pointY;
    }

    /**
     * negate
     */
    public final void negate() {
        pointX = -pointX;
        pointY = -pointY;
    }

    /**
     * offset
     *
     * @param dx
     * @param dy
     */
    public final void offset(float dx, float dy) {
        pointX += dx;
        pointY += dy;
    }

    /**
     * Returns true if the point's coordinates equal (x,y)
     *
     * @param pointX
     * @param pointY
     * @return boolean
     */
    public final boolean equals(float pointX, float pointY) {
        return this.pointX == pointX && this.pointY == pointY;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object == null || getClass() != object.getClass()) {
            return false;
        }

        PointF pointF = (PointF) object;

        if (Float.compare(pointF.pointX, pointX) != 0) {
            return false;
        }
        if (Float.compare(pointF.pointY, pointY) != 0) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = (pointX != +0.0f ? Float.floatToIntBits(pointX) : 0);
        result = 31 * result + (pointY != +0.0f ? Float.floatToIntBits(pointY) : 0);
        return result;
    }

    @Override
    public String toString() {
        return "PointF(" + pointX + ", " + pointY + ")";
    }

    /**
     * Return the euclidian distance from (0,0) to the point
     *
     * @return float
     */
    public final float length() {
        return length(pointX, pointY);
    }

    /**
     * Returns the euclidian distance from (0,0) to (x,y)
     *
     * @param lx
     * @param ly
     * @return float
     */
    public static float length(float lx, float ly) {
        return (float) Math.hypot(lx, ly);
    }
}

